/***************************************************************************
 * The Open Armory API is a project to retrieve World of Warcraft(R) 
 * related XML data from The Armory.
 * 
 * The data retrieved via the Open Armory API from The Armory is 
 * proprietary to Blizzard Entertainment, Inc(R).
 * *************************************************************************
 * Copyright (C) 2009 - 2010 Tedla M.B. Brandsema
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/
package net.openarmory.marshalling;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import net.openarmory.core.domain.ArmoryCharacterXmlRetriever;
import net.openarmory.core.domain.ArmoryGuildXmlRetriever;
import net.openarmory.core.domain.ArmoryItemXmlRetriever;
import net.openarmory.core.domain.url.ArmoryCharacterUrl;
import net.openarmory.core.domain.url.ArmoryGuildUrl;
import net.openarmory.core.domain.url.ArmoryItemUrl;
import net.openarmory.core.domain.url.ArmoryUrl;
import net.openarmory.marshalling.character.ArmoryCharacterPage;
import net.openarmory.marshalling.character.ArmoryCharacterPageObjectFactory;
import net.openarmory.marshalling.guild.ArmoryGuildPage;
import net.openarmory.marshalling.guild.ArmoryGuildPageObjectFactory;
import net.openarmory.marshalling.item.ArmoryItemPage;
import net.openarmory.marshalling.item.ArmoryItemPageObjectFactory;

import org.apache.log4j.Logger;

// TODO: Auto-generated Javadoc
/**
 * The Class ArmoryPageUnmarshaller.
 */
public final class ArmoryPageUnmarshaller {

	/** The Constant LOG. */
	private static final Logger LOG = Logger
			.getLogger(ArmoryPageUnmarshaller.class);

	/**
	 * Unmarshal page.
	 * 
	 * @param armoryUrl
	 *            the armory url
	 * @return the armory page
	 */
	public ArmoryPage unmarshalPage(final ArmoryUrl armoryUrl) {
		ArmoryPage armoryPage = null;
		Unmarshaller unmarshaller = null;
		InputStream is = null;
		JAXBContext jaxbCharacterContext = null;
		JAXBContext jaxbGuildContext = null;
		JAXBContext jaxbItemContext = null;
		try {
			jaxbCharacterContext = JAXBContext
					.newInstance(ArmoryCharacterPageObjectFactory.class);
			jaxbGuildContext = JAXBContext
					.newInstance(ArmoryGuildPageObjectFactory.class);
			jaxbItemContext = JAXBContext
					.newInstance(ArmoryItemPageObjectFactory.class);

			if (armoryUrl instanceof ArmoryCharacterUrl) {
				final ArmoryCharacterXmlRetriever acxr = new ArmoryCharacterXmlRetriever();
				is = acxr.fetchContent(((ArmoryCharacterUrl) armoryUrl)
						.getUrl());
				unmarshaller = jaxbCharacterContext.createUnmarshaller();
				unmarshaller.setProperty(
						"com.sun.xml.internal.bind.ObjectFactory",
						new ArmoryCharacterPageObjectFactory());
				armoryPage = (ArmoryCharacterPage) unmarshaller.unmarshal(is);
			} else if (armoryUrl instanceof ArmoryGuildUrl) {
				final ArmoryGuildXmlRetriever agxr = new ArmoryGuildXmlRetriever();
				is = agxr.fetchContent(((ArmoryGuildUrl) armoryUrl).getUrl());
				unmarshaller = jaxbGuildContext.createUnmarshaller();
				unmarshaller.setProperty(
						"com.sun.xml.internal.bind.ObjectFactory",
						new ArmoryGuildPageObjectFactory());
				armoryPage = (ArmoryGuildPage) unmarshaller.unmarshal(is);
			} else if (armoryUrl instanceof ArmoryItemUrl) {
				final ArmoryItemXmlRetriever aixr = new ArmoryItemXmlRetriever();
				is = aixr.fetchContent(((ArmoryItemUrl) armoryUrl).getUrl());
				unmarshaller = jaxbItemContext.createUnmarshaller();
				unmarshaller.setProperty(
						"com.sun.xml.internal.bind.ObjectFactory",
						new ArmoryItemPageObjectFactory());
				armoryPage = (ArmoryItemPage) unmarshaller.unmarshal(is);
			} else {
				throw new IllegalArgumentException("Unknown ArmoryUrl type");
			}

		} catch (final JAXBException e) {
			LOG.error("Unable to unmarshall " + armoryUrl.getUrl(), e);
		}

		return armoryPage;
	}
}
